package com.ilarele.shopping.client.adaptors;

import java.util.Comparator;
import java.util.Random;

import com.ilarele.shopping.Product;

public abstract class ProductComparator implements Comparator<Product> {
	protected int direction;
	public static final Random rand = new Random();

	@Override
	public int compare(Product p1, Product p2) {
		switch (direction) {
		case Sorter.SORT_DIR_ASC:
			return getComparedValue(p1, p2);
		case Sorter.SORT_DIR_DESC:
			return getComparedValue(p2, p1);
		default:
			return 1 - rand.nextInt(3);
		}
	}

	protected abstract int getComparedValue(Product p1, Product p2);

	public void setDirection(int sortDirection) {
		direction = sortDirection;
	}

}
